package com.example.ccj.b.c.draw;

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;

import com.example.ccj.R;
import com.example.ccj.u.StringUtils;

import java.sql.Struct;


public class TopBar extends LinearLayout {

    // topBar
    private LinearLayout tobBarContainer;// tapBar容器
    private ImageView topLeftImage;// 左侧图标
    private TextView topTitle;// 标题
    private TextView rightText;// 右侧文字
    private TextView leftText;// 左侧文字
    private ImageView topRightImage;// 右侧图标

    public TopBar(Context context) {
        super(context);
        init();
    }

    private String titleName;

    public TopBar(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.TopBar);
        titleName = typedArray.getString(R.styleable.TopBar_title);
        typedArray.recycle();
        init();
    }

    private void init(){
        LayoutInflater.from(getContext()).inflate(R.layout.tap_bar,this);
        // topBar
        tobBarContainer = findViewById(R.id.tobBarContainer);
        topLeftImage = findViewById(R.id.topLeftImg);// 返回按钮
        topTitle = findViewById(R.id.topTitle);// 标题
        topRightImage = findViewById(R.id.topRightImg);// 搜索
        rightText = findViewById(R.id.topRightText);// 右侧文字
        leftText = findViewById(R.id.topLeftText);// 左侧文字
        rightText.setVisibility(View.GONE);
        leftText.setVisibility(View.GONE);
        topRightImage.setVisibility(View.INVISIBLE);

        if (!StringUtils.isEmpty(titleName)) topTitle.setText(titleName);

        topLeftImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getContext() instanceof Activity) ((Activity) getContext()).onBackPressed();
            }
        });
    }

//    /**
//     * 构造函数
//     * */
//    public TopBar(Activity activity){
//        this.activity = activity;
//
//        topView = activity.findViewById(R.id.topBarLL);
//        // topBar
//        tobBarContainer = topView.findViewById(R.id.tobBarContainer);
//        topLeftImage = topView.findViewById(R.id.topLeftImg);// 返回按钮
//        topTitle = topView.findViewById(R.id.topTitle);// 标题
//        topRightImage = topView.findViewById(R.id.topRightImg);// 搜索
//    }

    /**
     * 设置标题
     * */
    public TopBar setTitle(String title){
        if (!title.isEmpty()){
            topTitle.setText(title);
        }
        return this;
    }

    public TextView setRightText(String text){
        if (!text.isEmpty()){
            if (leftText.getVisibility() == GONE) leftText.setVisibility(View.INVISIBLE);
            rightText.setVisibility(VISIBLE);
            rightText.setText(text);
        }
        return rightText;
    }


    public TextView setLeftText(String text){
        if (!text.isEmpty()){
            if (rightText.getVisibility() == GONE) rightText.setVisibility(View.INVISIBLE);
            leftText.setVisibility(VISIBLE);
            leftText.setText(text);
        }
        return leftText;
    }

    /**
     * 设置左侧图标
     * */
    public TopBar setLeftIco(int resId){
        topLeftImage.setVisibility(resId > 0 ? View.VISIBLE : View.GONE);
        topLeftImage.setImageResource(resId);
        return this;
    }



    /**
     * 左侧图标点击事件
     * */
    public TopBar setLeftClick(View.OnClickListener leftClick){
        if (topLeftImage.getVisibility() == View.VISIBLE){
            topLeftImage.setOnClickListener(leftClick);
        }
        return this;
    }

    /**
     * 设置右侧图标控件隐藏
     * */
    public TopBar setRightHidden(){
        if (topLeftImage.getVisibility() == View.VISIBLE){
            topRightImage.setVisibility(View.INVISIBLE);
        } else {
            topRightImage.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 设置右侧图标控件隐藏
     * */
    public TopBar setLeftImageHidden(){
        if (topRightImage.getVisibility() == View.VISIBLE){
            topLeftImage.setVisibility(View.INVISIBLE);
        } else {
            topLeftImage.setVisibility(View.GONE);
        }
        return this;
    }

    /**
     * 设置右侧图标
     * */
    public TopBar setRightIcon(int resId){
        if (resId > 0) {
            topRightImage.setVisibility(View.VISIBLE);
            topRightImage.setImageResource(resId);
            if (rightText.getVisibility() != VISIBLE) rightText.setVisibility(GONE);
            topRightImage.setImageResource(resId);
        }
        return this;
    }

    /**
     * 右侧图标点击事件
     * */
    public TopBar setRightClick(View.OnClickListener rightClick){
        if (topRightImage.getVisibility() == View.VISIBLE){
            topRightImage.setOnClickListener(rightClick);
        }
        return this;
    }


    public LinearLayout getTobBarContainer() {
        return tobBarContainer;
    }

    public ImageView getTopLeftImage() {
        return topLeftImage;
    }

    public TextView getTopTitle() {
        return topTitle;
    }

    public TextView getRightText() {
        return rightText;
    }

    public ImageView getTopRightImage() {
        return topRightImage;
    }

    public TextView getLeftText() {
        return leftText;
    }

    public String getTitleName() {
        return titleName;
    }
}